<?php
use Phalcon\Mvc\View;
use Phalcon\Crypt;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Logger\Formatter\Line as FormatterLine;
require APP_PATH . '/../libs/View.php';
/**
 * Register the global configuration as config
 */
$di->setShared('config', function () {
    return include APP_PATH . "/config/config.php";
});

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->setShared('url', function () {
    $config = $this->getConfig();

    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});

$di->setShared('view', function(){
    $config = $this->getConfig();
    $view = new \app\libs\View();
    $view->setViewsDir($config->application->viewsDir);
    return $view;
});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () {
    $config = $this->getConfig();
    $db = new DbAdapter([
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
    ]);
    $charset = $config->database->charset;
    $db->execute('set names ' . $charset);
    $eventManager = new Phalcon\Events\Manager();
    $dbEvents = new \app\events\DbEvents();
    $eventManager->attach('db', $dbEvents);
    $db->setEventsManager($eventManager);
    return $db;
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->setShared('modelsMetadata', function () {
    $config = $this->getConfig();
    return new Phalcon\Mvc\Model\MetaData\Files([
        'metaDataDir' => $config->application->cacheDir . 'metaData/'
    ]);
});

$di->setShared('modelsCache', function(){
    $frontCache = new \Phalcon\Cache\Frontend\Data([
        'lifetime' => 86400
    ]);
    $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, [
        'host' => 'localhost',
        'port' => 11211
    ]);
    return $cache;
});

$di->setShared('viewCache',function(){
    $frontCache = new \Phalcon\Cache\Frontend\Output([
        'lifetime' => 86400
    ]);
    $cache = new \Phalcon\Cache\Backend\File($frontCache, [
        'cacheDir' => APP_PATH . '/cache/views/'
    ]);
//    $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, [
//        'host' => 'localhost',
//        'port' => 11211
//    ]);
    return $cache;
});

$di->setShared('mongo', function(){
    $con = new MongoDB\Driver\Manager('mongodb://192.168.56.1');
    return $con;
});

$di->setShared('collectionManager', function(){
   $manager = new \Phalcon\Mvc\Collection\Manager();
    
    return $manager;
});

/**
 * Start the session the first time some component request the session service
 */
$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();
    return $session;
});

/**
 * Crypt service
 */
$di->setShared('crypt', function () {
    $config = $this->getConfig();

    $crypt = new Crypt();
    $crypt->setKey($config->application->cryptSalt);
    return $crypt;
});

/**
 * Dispatcher use a default namespace
 */
$di->setShared('dispatcher', function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('app\controllers');
    return $dispatcher;
});

/**
 * Loading routes from the routes.php file
 */
$di->setShared('router', function () {
    return require APP_PATH . '/config/routes.php';
});

$di->setShared('response', function() {
    $response = new Phalcon\Http\Response;
    return $response;
});

/**
 * Logger service
 */
$di->setShared('logger', function ($filename = null, $format = null) {
    $config = $this->getConfig();

    $format   = $format ?: $config->get('logger')->format;
    $filename = date('Y-m-d') . '.log';
    $path     = $config->get('logger')->path . $filename;
    $filename = str_replace('\\', '/', $path);

    $formatter = new FormatterLine($format, $config->get('logger')->date);
    $logger    = new \Phalcon\Logger\Adapter\Stream($filename);

    $logger->setFormatter($formatter);
    $logger->setLogLevel($config->get('logger')->logLevel);

    return $logger;
});
